<?php
/**
 * Created by PhpStorm.
 * User: lzy 林子彦
 * Date: 2018/6/1
 * Time: 上午1:20
 */

namespace App\Admin\Repositories;

use App\Admin\Contracts\ActionInterface;
use App\Admin\Models\ActionLogModel;
use App\Admin\Models\ActionModel;
use Illuminate\Support\Facades\Request;

/**
 *  行为动作实现
 * Class DbConfigRepository
 * @package App\Admin\Repositories
 */
class DbActionRepository implements ActionInterface
{
    /**
     * 用户行为模型
     * @return ActionModel
     */
    public function getActionModel()
    {
        return app()->make(ActionModel::class);
    }

    /**
     * 行为日志记录表
     * @return ActionLogModel
     */
    public function getActionLogModel(){
        return app()->make(ActionLogModel::class);
    }

    /**
     * 行为列表
     * @param $where
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getActionList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        return $this->getActionModel()->newQuery()->where($where)->paginate($limit);
    }

    /**
     * 信息
     * @param $where
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getActionInfo($where){
        return $this->getActionModel()->newQuery()->where($where)->first();
    }
    public function delActionInfo($where){
        return $this->getActionModel()->newQuery()->where($where)->delete();
    }
    public function saveActionLogData($data){
        return $this->getActionLogModel()->newQuery()->create($data);
    }
    /**
     * 添加与更新
     * @param $data
     */
    public function updateOrCreateByData($data)
    {
        $id = $data["id"];
        unset($data["id"]);
        $data = $this->getActionModel()->filter($data);
        return $this->getActionModel()->newQuery()->updateOrCreate(["id" => $id], $data);
    }

    /**
     * 根据名字获取日志列表
     * @param $name
     */
    public function getActionLogByActionName($name){
      $actionInfo=$this->getActionModel()->newQuery()->where(["name"=>$name])->first();
      //获取列表返回
       return $this->getActionLogModel()->newQuery()->where("action_id",$actionInfo->id)->orderBy("create_time","desc")->limit(15)->get();
    }

    /**
     * 根据条件取可用action  id 然后返 action log
     * @param $where
     */
    public function getActionLogByActionList($where,$data){
       $actionIds=$this->getActionModel()->newQuery()->where($where)->pluck("id");
       $where2=function($query) use ($actionIds,$data){
           $query->whereIn("action_id",$actionIds);
           if(!empty($data["start_date"])){
               $query->where("create_time", ">", strtotime($data["start_date"]));
           }
           if(!empty($data["end_date"])){
               $query->where("create_time", "<", strtotime($data["end_date"]." 23:59:59"));
           }
       };
       return $this->getActionLogList($where2);
    }

    /**
     * 行为日志表
     * @param $where
     */
    public function getActionLogList($where){
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        return $this->getActionLogModel()->newQuery()->where($where)->paginate($limit);
    }

    /**
     * 日志行为删除
     * @param $where
     */
    public function delActionLogInfo($where){
        return $this->getActionLogModel()->newQuery()->where($where)->delete();
    }
}